fatstart db 0,0
fatend db 0,0
nextfat db 0,0

newfat:
	mov ax,64
	call maloc
	mov [fatstart],bx
	mov [fatend],ax	
	add bx,1
	mov [nextfat],bx
ret

newfile:			;Write file using bFAT	IN - si, file name, ax, file length
	mov di,si
	push si
	call maloc
	sub ax,[fatend]
	sub bx,[fatend]
	mov si,[nextfat]
	mov [si],bx
	add si,1
	mov [si],ax
	add si,1
	xchg di,si
	call copystring
	mov ax,di
	call length
	add di,ax
	add di,2
	mov [nextfat],di
	pop si
	call findfile
	mov si,ax
.fill
	cmp si,bx
	je .done
	add si,1
	mov byte [si],0
	jmp .fill
.done
ret

killfile:
	call findfile
	push si
	mov dx,bx
	mov si,ax
	call memclear	
	pop si
.clearfat
	cmp byte [si],'*'
	je .done
	mov byte [si],'*'
	add si,1
	jmp .clearfat
.done
ret

filelist:			;Print list of files
	mov si,[fatstart]
.loop
	cmp byte [si],'*'
	je .found
	add si,1
	cmp si,[nextfat]
	jge .done
	jmp .loop
.found
	add si,3
	cmp byte [si],'*'
	je .done
	call print
	call printret
	jmp .loop
.done
ret

findfile:					;Find file and give location
	mov ax,0
	mov bx,0
	mov si,[fatstart]
.find
	cmp byte [si],'*'
	je .file
	add si,1
	cmp si,[fatend]
	jge .done
	jmp .find
.file	
	add si,3
	call compare
	jc .found
	jmp .find
.found
	sub si,1
	mov bl,[si]
	add bx,[fatend]	
	sub si,1
	mov al,[si]
	add ax,[fatend]
.done	
ret

fileman:
.loop
	mov si,.prmpt
	call print
	mov di,buffer
	call input
	
	mov di,buffer
	mov si,quit
	call compare
	jc .done

	mov si,.new
	call compare
	jc .newcmd

	mov si,.list
	call compare
	jc .listcmd

	mov si,.find
	call compare
	jc .findcmd

	mov si,.edit
	call compare
	jc .editcmd

	mov si,.type
	call compare
	jc .typecmd

	mov si,.kill
	call compare
	jc .killcmd

	jmp .loop

.newcmd
	call usernew
	jmp .loop
.listcmd
	call filelist
	jmp .loop
.findcmd
	call userfind
	jmp .loop
.editcmd
	call useredit
	jmp .loop
.typecmd
	call usertype
	jmp .loop
.killcmd
	call userkill
	jmp .loop
.done	
ret
	.prmpt db 'bFAT>',0
	.new db 'new',0
	.list db 'list',0
	.find db 'find',0
	.edit db 'edit',0
	.type db 'type',0
	.kill db 'kill',0

usernew:
	mov si,.nameprmpt
	call print
	mov di,.tmp1
	call input

	mov si,.sizeprmpt
	call print
	mov di,buffer
	call input
	mov si,buffer
	call toint
	
	mov si,.tmp1
	call getsystime
	call newfile
	call getsystime
ret
	.nameprmpt db 'NAME>',0
	.sizeprmpt db 'SIZE>',0
	.tmp1 times 16 db 0

userfind:
	mov si,.nameprmpt
	call print
	mov di,buffer
	call input

	mov di,buffer
	call getsystime
	call findfile
	call getsystime

	mov dx,bx
	call tostring
	mov si,ax
	call print
	mov si,.space
	call print

	mov ax,dx
	call tostring
	mov si,ax
	call print
	call printret
ret
	.nameprmpt db 'NAME>',0	
	.space db ' - ',0

useredit:
	mov si,.name
	call print
	mov di,buffer
	call input

	mov di,buffer
	call findfile
	push ax
	push bx

	mov si,.prmpt
	call print
	mov di,buffer
	call input
	
	pop ax
	pop bx
	mov si,buffer
	mov di,bx
	call copystring
.done

ret
	.name db 'NAME>',0
	.prmpt db '>',0

usertype:
	mov si,.name
	call print
	mov di,buffer
	call input
	
	mov di,buffer
	call findfile
	
	mov si,ax
	call print
	call printret
ret
	.name db 'NAME>',0

userkill:
	mov si,.name
	call print
	mov di,buffer
	call input

	mov di,buffer
	call killfile
ret
	.name db 'NAME>',0


getsystime:
	pusha
	pushfd
	cli
	mov al,00000000b
	out 43h,al
	in al,40h
	mov ah,al
	in al,40h
	rol ax,8
	sti
	popfd
	call tostring
	mov si,ax
	call print
	call printret
	popa
ret